﻿using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SXFramWork.Core;
using SXFramWork.Model;
using SXFramWork.Service;

namespace SXFramWork.API.Controllers.Admin
{
    /// <summary>
    /// 字典管理
    /// </summary>
    public class SysDictionaryController : WebController
    {
        private readonly ISysDictionaryService _dicService;
        public SysDictionaryController(ISysDictionaryService sysDictionaryService)
        {
            _dicService = sysDictionaryService;
        }

        /// <summary>
        /// 字典列表
        /// </summary>
        /// <returns></returns>
        [Menu("通用字典")]
        [HttpGet]
        public async Task<IApiResult> List([FromQuery] string name)
        {
            return await _dicService.Query(name);
        }
        /// <summary>
        /// 查询所有的头结点(parentid=0)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IApiResult> QueryParents()
        {
            return await _dicService.QueryParents();
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Power("添加")]
        public async Task<IApiResult> Add([FromBody] DictionaryAddModel model)
        {
            return await _dicService.Add(model);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        [Power("删除")]
        public async Task<IApiResult> Delete([FromBody]List<string> ids)
        {
            return await _dicService.Delete(ids);
        }
        /// <summary>
        /// 查询单个字典
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Power("查询单个字典")]
        public async Task<IApiResult> Get([FromQuery] string id)
        {
            return await _dicService.GetById(id);
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Power("更新")]
        public async Task<IApiResult> Update([FromBody] DictionaryUpdateModel model)
        {
            return await _dicService.Update(model);
        }

    }
}